package org.igeek.atomqq.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.igeek.atomqq.AtomQQApplication;
import org.igeek.atomqq.R;
import org.igeek.atomqq.adapter.MessageFriendListAdapter;
import org.igeek.atomqq.adapter.MessageGroupListAdapter;
import org.igeek.atomqq.net.Response;
import org.igeek.atomqq.service.MessageReceiver;
import org.igeek.atomqq.service.MessageReceiver.OnMessageRecevied;
import org.igeek.atomqq.task.CachedAsyncTask;
import org.igeek.atomqq.task.MessagePoll;
import org.igeek.atomqq.task.SendFriendMessageTask;
import org.igeek.atomqq.task.SendFriendMessageTask.SendFriendMessageResponse;
import org.igeek.atomqq.task.SendGroupMessageTask;
import org.igeek.atomqq.task.SendGroupMessageTask.SendGroupMessageResponse;
import org.igeek.atomqq.task.TaskResultListener;
import org.igeek.atomqq.vo.Chat;
import org.igeek.atomqq.vo.MsgChat;
import org.igeek.atomqq.vo.QQMessage;
import org.igeek.atomqq.vo.MsgFriend;
import org.igeek.atomqq.vo.MsgFriendListChange;
import org.igeek.atomqq.vo.MsgFriendStatus;
import org.igeek.atomqq.vo.MsgGroup;
import org.igeek.atomqq.vo.MsgSysGroup;
import org.igeek.atomqq.widget.AlwaysMarqueeTextView;

import android.app.SearchManager.OnCancelListener;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

/** 
 * 聊天窗口 
 * @author <a href="http://hangxin1940.cnblogs.com">hangxin1940@gmail.com</a>
 * @time 2012-2-11 下午08:51:33 
 */
public class CommonChat extends BaseActivity implements OnMessageRecevied,OnClickListener ,TaskResultListener{
	
	public static final String UIN="uin";
	public static final String TYPE="type";
	
	private static final int FRIEND=0x1;
	private static final int GROUP=0x2;
	
	private ImageView ivUserFace;
	private AlwaysMarqueeTextView amtvUserLnick;
	private AlwaysMarqueeTextView amtvUserName;
	private ListView lvMessage;
	
	private EditText etMessage;
	private Button btnSubmit;
	
	private BaseAdapter adapterChats;
	
	private MessageReceiver messageReceiver;
	
	private int type;
	
	private long uin;
	
	private AtomQQApplication application;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_comchat);
		
		ivUserFace=(ImageView) findViewById(R.id.comchat_ivUserFace);
		amtvUserLnick=(AlwaysMarqueeTextView) findViewById(R.id.comchat_amtvUserLnick);
		amtvUserName=(AlwaysMarqueeTextView) findViewById(R.id.comchat_amtvUserName);
		lvMessage=(ListView) findViewById(R.id.comchat_lvMessages);
		etMessage=(EditText) findViewById(R.id.comchat_etMessage);
		btnSubmit=(Button) findViewById(R.id.comchat_btnSubmit);
		
		btnSubmit.setOnClickListener(this);
		
		uin=getIntent().getLongExtra(UIN, 0);
		type=getIntent().getIntExtra(TYPE, 0);
		
		application=(AtomQQApplication) getApplication();
		
		if(type==Chat.FRIEND){
			adapterChats=new MessageFriendListAdapter(this, uin);
			application.getFriend(uin).setNewMessage(0);
			
			
		}else{
			adapterChats=new MessageGroupListAdapter(this, uin);
			application.getGroup(uin).setNewMessage(0);
		}
		
		lvMessage.setAdapter(adapterChats);
		messageReceiver =new MessageReceiver(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		IntentFilter broadCastFilter=new IntentFilter();
		if(type==Chat.FRIEND)
			broadCastFilter.addAction(MessagePoll.BROADCAST_MSG_FRIEND);
		else
			broadCastFilter.addAction(MessagePoll.BROADCAST_MSG_GROUP);
		broadCastFilter.setPriority(800);
		
		registerReceiver(messageReceiver, broadCastFilter, MessageReceiver.PERMISSION, null);
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(messageReceiver);
	}

	@Override
	public void handleMessage(int type, QQMessage message) {
		switch (type) {
		case MessageReceiver.BROADCAST_TYPE_FRIEND:
			adapterChats.notifyDataSetChanged();
			
			break;

			
			
		case MessageReceiver.BROADCAST_TYPE_GROUP:
			adapterChats.notifyDataSetChanged();
			
			
			break;
			

		}
	}

	@Override
	public void onClick(View v) {
		btnSubmit.setEnabled(false);
		etMessage.setEnabled(false);
		
		if(type==Chat.FRIEND){
			SendFriendMessageTask task=new SendFriendMessageTask(this);
			task.setOnTaskResult(this, FRIEND);
			task.setParams(uin, etMessage.getText().toString());
			task.execute();
		}else{
			
			SendGroupMessageTask task=new SendGroupMessageTask(this);
			task.setOnTaskResult(this, GROUP);
			task.setParams(application.getGroup(uin).getGid(), etMessage.getText().toString());
			task.execute();
		}
	}

	@Override
	public void onTaskResult(Response response, int flag) {
		btnSubmit.setEnabled(true);
		etMessage.setEnabled(true);
		
		switch (flag) {
		case FRIEND:
			SendFriendMessageResponse res=(SendFriendMessageResponse) response;
			
			if(res.getSuccsess()){
				String message=etMessage.getText().toString();
				
				List<MsgChat> chats=new ArrayList<MsgChat>();
				MsgChat chat=new MsgChat();
				chat.setContent(message);
				chats.add(chat);
				
				MsgFriend msg=new MsgFriend();
				msg.setTime(new Date().getTime());
				msg.setFrom_uin(0);
				msg.setMessageContents(chats);
				
				
				
				
				Chat chatFriend=new Chat();
				chatFriend.setType(Chat.FRIEND);
				chatFriend.setUin(uin);
				chatFriend.setMessage(msg);
				
				application.putChat(uin, chatFriend);
				etMessage.setText("");
			}
			break;

		case GROUP:
			SendGroupMessageResponse resg=(SendGroupMessageResponse) response;
			
			if(resg.getSuccsess()){
				String message=etMessage.getText().toString();
				
				List<MsgChat> chats=new ArrayList<MsgChat>();
				MsgChat chat=new MsgChat();
				chat.setContent(message);
				chats.add(chat);
				
				MsgGroup msg=new MsgGroup();
				msg.setTime(new Date().getTime());
				msg.setFrom_uin(0);
				msg.setMessageContents(chats);
				
				
				
				
				Chat chatFriend=new Chat();
				chatFriend.setType(Chat.FRIEND);
				chatFriend.setGcode(uin);
				chatFriend.setMessage(msg);
				
				application.putChat(uin, chatFriend);
				etMessage.setText("");
			}
			break;
		}
		
		
	}
}
